// merge 1:1 ts_id using "$datapath\Eleanor\Control Groups\controlgroup", nogen keepusing(level* repeat*)



clear all
set more off, perm
capture log close

set seed 88

set maxvar 32767
//global klmChinaReach2: env klmChinaReach2
//global datapath "${klmChinaReach2}/Fuyao_Erlfang"
cd "C:\Users\msjin\OneDrive\Desktop\measurement\"

use "data_task_panel_cluster_May31_m.dta", clear
capture drop _merge
sort KIDID 


forvalues j=1/12{
	bys KIDID: egen temp`j'= max(paratec2) if levelc2==`j' 
	bys KIDID: egen ave_pass_c_`j'=max(temp`j') if levelc2!=.
	drop temp`j'
}
cap drop ave_pass_c_c
gen ave_pass_c_c =.
forvalues j=1/12{
	replace ave_pass_c_c=ave_pass_c_`j' if levelc2==`j' & ave_pass_c_c ==.
}


drop if levelc2==.



collapse (mean) ave_pass_c_c ave_pass_c_1 ave_pass_c_2 ave_pass_c_3 ave_pass_c_4 ave_pass_c_5 ave_pass_c_6 ave_pass_c_7 ave_pass_c_8 ave_pass_c_9 ave_pass_c_10 ave_pass_c_11 ave_pass_c_12, by(KIDID levelc2)

keep if levelc2==1

drop levelc2

save "rv_across_level_c.dta",replace




clear all
set more off, perm
capture log close

set seed 88

set maxvar 32767
//global klmChinaReach2: env klmChinaReach2
//global datapath "${klmChinaReach2}/Fuyao_Erlfang"
cd "C:\Users\msjin\OneDrive\Desktop\measurement\"

use "data_task_panel_cluster_May31_fine.dta", clear
capture drop _merge
sort KIDID 


forvalues j=1/7{
	bys KIDID: egen temp`j'= max(paratef2) if levelf2==`j' 
	bys KIDID: egen ave_pass_f_`j'=max(temp`j') if levelf2!=.
	drop temp`j'
}
cap drop ave_pass_f_f
gen ave_pass_f_f =.
forvalues j=1/7{
	replace ave_pass_f_f=ave_pass_f_`j' if levelf2==`j' & ave_pass_f_f ==.
}



drop if levelf2==.



collapse (mean) ave_pass_f_f ave_pass_f_1 ave_pass_f_2 ave_pass_f_3 ave_pass_f_4 ave_pass_f_5 ave_pass_f_6 ave_pass_f_7, by(KIDID levelf2)


keep if levelf2==1

drop levelf2

save "rv_across_level_f.dta",replace

use "rv_across_level_f.dta", clear

foreach s in f{
tempfile `s'_across_level
tempname table
postfile `table' str8(fine) str8(quantile) str8(Category) G1 G2 G3 G4 G5 G6 G7 using `s'_across_level_q1, replace

forvalues i=1/7{
	//local k=`i'+1
	forvalues j=1/7{
		if (`j'==`i') {
			local l_q11_`i'_`j'=-99
			local l_q12_`i'_`j'=-99
			local l_q13_`i'_`j'=-99
			local l_q14_`i'_`j'=-99
			local N_q1_`i'_`j'=-99
			local f_q11_`i'_`j'=-99
			local f_q12_`i'_`j'=-99
			local f_q13_`i'_`j'=-99
			local f_q14_`i'_`j'=-99
		} 
		else {
		count if ave_pass_f_`i'>=0.75 & ave_pass_f_`j'>=0.75 & ave_pass_f_`i'!=. & ave_pass_f_`j'!=.
		local  l_q11_`i'_`j'=`r(N)'
		count  if ave_pass_f_`i'>=0.75 & ave_pass_f_`j'<0.75  & ave_pass_f_`j'>=0.5  & ave_pass_f_`i'!=. & ave_pass_f_`j'!=.
	    local l_q12_`i'_`j'=`r(N)'
		count if ave_pass_f_`i'>=0.75 & ave_pass_f_`j'<0.5   & ave_pass_f_`j'>=0.25 & ave_pass_f_`i'!=. & ave_pass_f_`j'!=.
		local l_q13_`i'_`j'=`r(N)'
		count if ave_pass_f_`i'>=0.75 & ave_pass_f_`j'<0.25  & ave_pass_f_`i'!=. & ave_pass_f_`j'!=.
		local l_q14_`i'_`j'=`r(N)'
		count if ave_pass_f_`i'>=0.75 & ave_pass_f_`i'!=. & ave_pass_f_`j'!=.
		local N_q1_`i'_`j'=`r(N)'
		forvalues mm = 1/4 {
			local f_q1`mm'_`i'_`j' = `l_q1`mm'_`i'_`j''/`N_q1_`i'_`j''
		}
		}
		
	}

	post `table' ("`i'") ("1") ("q11")  (`l_q11_`i'_1') (`l_q11_`i'_2') (`l_q11_`i'_3') (`l_q11_`i'_4') (`l_q11_`i'_5') (`l_q11_`i'_6') (`l_q11_`i'_7')
	post `table' ("`i'") ("1") ("q12") (`l_q12_`i'_1') (`l_q12_`i'_2') (`l_q12_`i'_3') (`l_q12_`i'_4') (`l_q12_`i'_5') (`l_q12_`i'_6') (`l_q12_`i'_7') 
	post `table' ("`i'") ("1") ("q13") (`l_q13_`i'_1') (`l_q13_`i'_2') (`l_q13_`i'_3') (`l_q13_`i'_4') (`l_q13_`i'_5') (`l_q13_`i'_6') (`l_q13_`i'_7') 
	post `table' ("`i'") ("1") ("q14") (`l_q14_`i'_1') (`l_q14_`i'_2') (`l_q14_`i'_3') (`l_q14_`i'_4') (`l_q14_`i'_5') (`l_q14_`i'_6') (`l_q14_`i'_7')
	post `table' ("`i'") ("1") ("N")  (`N_q1_`i'_1') (`N_q1_`i'_2') (`N_q1_`i'_3') (`N_q1_`i'_4') (`N_q1_`i'_5') (`N_q1_`i'_6') (`N_q1_`i'_7')
	post `table' ("`i'") ("1") ("f11") (`f_q11_`i'_1') (`f_q11_`i'_2') (`f_q11_`i'_3') (`f_q11_`i'_4') (`f_q11_`i'_5') (`f_q11_`i'_6') (`f_q11_`i'_7') 
	post `table' ("`i'") ("1") ("f12") (`f_q12_`i'_1') (`f_q12_`i'_2') (`f_q12_`i'_3') (`f_q12_`i'_4') (`f_q12_`i'_5') (`f_q12_`i'_6') (`f_q12_`i'_7') 
	post `table' ("`i'") ("1") ("f13") (`f_q13_`i'_1') (`f_q13_`i'_2') (`f_q13_`i'_3') (`f_q13_`i'_4') (`f_q13_`i'_5') (`f_q13_`i'_6') (`f_q13_`i'_7') 
	post `table' ("`i'") ("1") ("f14") (`f_q14_`i'_1') (`f_q14_`i'_2') (`f_q14_`i'_3') (`f_q14_`i'_4') (`f_q14_`i'_5') (`f_q14_`i'_6') (`f_q14_`i'_7')
}

postclose `table'

}

foreach s in f{
tempfile `s'_across_level
tempname table
postfile `table' str8(fine) str8(quantile) str8(Category) G1 G2 G3 G4 G5 G6 G7 using `s'_across_level_q2, replace

forvalues i=1/7{
	//local k=`i'+1
	forvalues j=1/7{
		if (`j'==`i') {
			local l_q21_`i'_`j'=-99
			local l_q22_`i'_`j'=-99
			local l_q23_`i'_`j'=-99
			local l_q24_`i'_`j'=-99
			local N_q2_`i'_`j'=-99
			local f_q21_`i'_`j'=-99
			local f_q22_`i'_`j'=-99
			local f_q23_`i'_`j'=-99
			local f_q24_`i'_`j'=-99
		} 
		else {
		count if ave_pass_f_`i'<0.75 & ave_pass_f_`i' >= 0.5 & ave_pass_f_`j'>=0.75 & ave_pass_f_`i'!=. & ave_pass_f_`j'!=.
		local  l_q21_`i'_`j'=`r(N)'
		count  if ave_pass_f_`i'<0.75 & ave_pass_f_`i' >= 0.5 & ave_pass_f_`j'<0.75  & ave_pass_f_`j'>=0.5  & ave_pass_f_`i'!=. & ave_pass_f_`j'!=.
	    local l_q22_`i'_`j'=`r(N)'
		count if ave_pass_f_`i'<0.75 & ave_pass_f_`i' >= 0.5 & ave_pass_f_`j'<0.5   & ave_pass_f_`j'>=0.25 & ave_pass_f_`i'!=. & ave_pass_f_`j'!=.
		local l_q23_`i'_`j'=`r(N)'
		count if ave_pass_f_`i'<0.75 & ave_pass_f_`i' >= 0.5 & ave_pass_f_`j'<0.25  & ave_pass_f_`i'!=. & ave_pass_f_`j'!=.
		local l_q24_`i'_`j'=`r(N)'
		count if ave_pass_f_`i'<0.75 & ave_pass_f_`i' >= 0.5 & ave_pass_f_`i'!=. & ave_pass_f_`j'!=.
		local N_q2_`i'_`j'=`r(N)'
		forvalues mm = 1/4 {
			local f_q2`mm'_`i'_`j' = `l_q2`mm'_`i'_`j''/`N_q2_`i'_`j''
		}
		}
		
	}

	post `table' ("`i'") ("2") ("q21")  (`l_q21_`i'_1') (`l_q21_`i'_2') (`l_q21_`i'_3') (`l_q21_`i'_4') (`l_q21_`i'_5') (`l_q21_`i'_6') (`l_q21_`i'_7')
	post `table' ("`i'") ("2") ("q22") (`l_q22_`i'_1') (`l_q22_`i'_2') (`l_q22_`i'_3') (`l_q22_`i'_4') (`l_q22_`i'_5') (`l_q22_`i'_6') (`l_q22_`i'_7') 
	post `table' ("`i'") ("2") ("q23") (`l_q23_`i'_1') (`l_q23_`i'_2') (`l_q23_`i'_3') (`l_q23_`i'_4') (`l_q23_`i'_5') (`l_q23_`i'_6') (`l_q23_`i'_7') 
	post `table' ("`i'") ("2") ("q24") (`l_q24_`i'_1') (`l_q24_`i'_2') (`l_q24_`i'_3') (`l_q24_`i'_4') (`l_q24_`i'_5') (`l_q24_`i'_6') (`l_q24_`i'_7')
	post `table' ("`i'") ("2") ("N")  (`N_q2_`i'_1') (`N_q2_`i'_2') (`N_q2_`i'_3') (`N_q2_`i'_4') (`N_q2_`i'_5') (`N_q2_`i'_6') (`N_q2_`i'_7')
	post `table' ("`i'") ("2") ("f21") (`f_q21_`i'_1') (`f_q21_`i'_2') (`f_q21_`i'_3') (`f_q21_`i'_4') (`f_q21_`i'_5') (`f_q21_`i'_6') (`f_q21_`i'_7') 
	post `table' ("`i'") ("2") ("f22") (`f_q22_`i'_1') (`f_q22_`i'_2') (`f_q22_`i'_3') (`f_q22_`i'_4') (`f_q22_`i'_5') (`f_q22_`i'_6') (`f_q22_`i'_7') 
	post `table' ("`i'") ("2") ("f23") (`f_q23_`i'_1') (`f_q23_`i'_2') (`f_q23_`i'_3') (`f_q23_`i'_4') (`f_q23_`i'_5') (`f_q23_`i'_6') (`f_q23_`i'_7') 
	post `table' ("`i'") ("2") ("f24") (`f_q24_`i'_1') (`f_q24_`i'_2') (`f_q24_`i'_3') (`f_q24_`i'_4') (`f_q24_`i'_5') (`f_q24_`i'_6') (`f_q24_`i'_7')
}

postclose `table'

}

foreach s in f{
tempfile `s'_across_level
tempname table
postfile `table' str8(fine) str8(quantile) str8(Category) G1 G2 G3 G4 G5 G6 G7 using `s'_across_level_q3, replace

forvalues i=1/7{
	//local k=`i'+1
	forvalues j=1/7{
		if (`j'==`i') {
			local l_q31_`i'_`j'=-99
			local l_q32_`i'_`j'=-99
			local l_q33_`i'_`j'=-99
			local l_q34_`i'_`j'=-99
			local N_q3_`i'_`j'=-99
			local f_q31_`i'_`j'=-99
			local f_q32_`i'_`j'=-99
			local f_q33_`i'_`j'=-99
			local f_q34_`i'_`j'=-99
		} 
		else {
		count if ave_pass_f_`i'<0.5 & ave_pass_f_`i' >= 0.25 & ave_pass_f_`j'>=0.75 & ave_pass_f_`i'!=. & ave_pass_f_`j'!=.
		local  l_q31_`i'_`j'=`r(N)'
		count  if ave_pass_f_`i'<0.5 & ave_pass_f_`i' >= 0.25 & ave_pass_f_`j'<0.75  & ave_pass_f_`j'>=0.5  & ave_pass_f_`i'!=. & ave_pass_f_`j'!=.
	    local l_q32_`i'_`j'=`r(N)'
		count if ave_pass_f_`i'<0.5 & ave_pass_f_`i' >= 0.25 & ave_pass_f_`j'<0.5   & ave_pass_f_`j'>=0.25 & ave_pass_f_`i'!=. & ave_pass_f_`j'!=.
		local l_q33_`i'_`j'=`r(N)'
		count if ave_pass_f_`i'<0.5 & ave_pass_f_`i' >= 0.25 & ave_pass_f_`j'<0.25  & ave_pass_f_`i'!=. & ave_pass_f_`j'!=.
		local l_q34_`i'_`j'=`r(N)'
		count if ave_pass_f_`i'<0.5 & ave_pass_f_`i' >= 0.25 & ave_pass_f_`i'!=. & ave_pass_f_`j'!=.
		local N_q3_`i'_`j'=`r(N)'
		forvalues mm = 1/4 {
			local f_q3`mm'_`i'_`j' = `l_q3`mm'_`i'_`j''/`N_q3_`i'_`j''
		}
		}
		
	}

	post `table' ("`i'") ("3") ("q31")  (`l_q31_`i'_1') (`l_q31_`i'_2') (`l_q31_`i'_3') (`l_q31_`i'_4') (`l_q31_`i'_5') (`l_q31_`i'_6') (`l_q31_`i'_7')
	post `table' ("`i'") ("3") ("q32") (`l_q32_`i'_1') (`l_q32_`i'_2') (`l_q32_`i'_3') (`l_q32_`i'_4') (`l_q32_`i'_5') (`l_q32_`i'_6') (`l_q32_`i'_7') 
	post `table' ("`i'") ("3") ("q33") (`l_q33_`i'_1') (`l_q33_`i'_2') (`l_q33_`i'_3') (`l_q33_`i'_4') (`l_q33_`i'_5') (`l_q33_`i'_6') (`l_q33_`i'_7') 
	post `table' ("`i'") ("3") ("q34") (`l_q34_`i'_1') (`l_q34_`i'_2') (`l_q34_`i'_3') (`l_q34_`i'_4') (`l_q34_`i'_5') (`l_q34_`i'_6') (`l_q34_`i'_7')
	post `table' ("`i'") ("3") ("N")  (`N_q3_`i'_1') (`N_q3_`i'_2') (`N_q3_`i'_3') (`N_q3_`i'_4') (`N_q3_`i'_5') (`N_q3_`i'_6') (`N_q3_`i'_7')
	post `table' ("`i'") ("3") ("f31") (`f_q31_`i'_1') (`f_q31_`i'_2') (`f_q31_`i'_3') (`f_q31_`i'_4') (`f_q31_`i'_5') (`f_q31_`i'_6') (`f_q31_`i'_7') 
	post `table' ("`i'") ("3") ("f32") (`f_q32_`i'_1') (`f_q32_`i'_2') (`f_q32_`i'_3') (`f_q32_`i'_4') (`f_q32_`i'_5') (`f_q32_`i'_6') (`f_q32_`i'_7') 
	post `table' ("`i'") ("3") ("f33") (`f_q33_`i'_1') (`f_q33_`i'_2') (`f_q33_`i'_3') (`f_q33_`i'_4') (`f_q33_`i'_5') (`f_q33_`i'_6') (`f_q33_`i'_7') 
	post `table' ("`i'") ("3") ("f34") (`f_q34_`i'_1') (`f_q34_`i'_2') (`f_q34_`i'_3') (`f_q34_`i'_4') (`f_q34_`i'_5') (`f_q34_`i'_6') (`f_q34_`i'_7')
}

postclose `table'

}

foreach s in f{
tempfile `s'_across_level
tempname table
postfile `table' str8(fine) str8(quantile) str8(Category) G1 G2 G3 G4 G5 G6 G7 using `s'_across_level_q4, replace

forvalues i=1/7{
	//local k=`i'+1
	forvalues j=1/7{
		if (`j'==`i') {
			local l_q41_`i'_`j'=-99
			local l_q42_`i'_`j'=-99
			local l_q43_`i'_`j'=-99
			local l_q44_`i'_`j'=-99
			local N_q4_`i'_`j'=-99
			local f_q41_`i'_`j'=-99
			local f_q42_`i'_`j'=-99
			local f_q43_`i'_`j'=-99
			local f_q44_`i'_`j'=-99
		} 
		else {
		count if ave_pass_f_`i'<0.25 & ave_pass_f_`j'>=0.75 & ave_pass_f_`i'!=. & ave_pass_f_`j'!=.
		local  l_q41_`i'_`j'=`r(N)'
		count  if ave_pass_f_`i'<0.25 & ave_pass_f_`j'<0.75  & ave_pass_f_`j'>=0.5  & ave_pass_f_`i'!=. & ave_pass_f_`j'!=.
	    local l_q42_`i'_`j'=`r(N)'
		count if ave_pass_f_`i'<0.25 & ave_pass_f_`j'<0.5   & ave_pass_f_`j'>=0.25 & ave_pass_f_`i'!=. & ave_pass_f_`j'!=.
		local l_q43_`i'_`j'=`r(N)'
		count if ave_pass_f_`i'<0.25 & ave_pass_f_`j'<0.25  & ave_pass_f_`i'!=. & ave_pass_f_`j'!=.
		local l_q44_`i'_`j'=`r(N)'
		count if ave_pass_f_`i'<0.25 & ave_pass_f_`i'!=. & ave_pass_f_`j'!=.
		local N_q4_`i'_`j'=`r(N)'
		forvalues mm = 1/4 {
			local f_q4`mm'_`i'_`j' = `l_q4`mm'_`i'_`j''/`N_q4_`i'_`j''
		}
		}
		
	}

	post `table' ("`i'") ("4") ("q41")  (`l_q41_`i'_1') (`l_q41_`i'_2') (`l_q41_`i'_3') (`l_q41_`i'_4') (`l_q41_`i'_5') (`l_q41_`i'_6') (`l_q41_`i'_7')
	post `table' ("`i'") ("4") ("q42") (`l_q42_`i'_1') (`l_q42_`i'_2') (`l_q42_`i'_3') (`l_q42_`i'_4') (`l_q42_`i'_5') (`l_q42_`i'_6') (`l_q42_`i'_7') 
	post `table' ("`i'") ("4") ("q43") (`l_q43_`i'_1') (`l_q43_`i'_2') (`l_q43_`i'_3') (`l_q43_`i'_4') (`l_q43_`i'_5') (`l_q43_`i'_6') (`l_q43_`i'_7') 
	post `table' ("`i'") ("4") ("q44") (`l_q44_`i'_1') (`l_q44_`i'_2') (`l_q44_`i'_3') (`l_q44_`i'_4') (`l_q44_`i'_5') (`l_q44_`i'_6') (`l_q44_`i'_7')
	post `table' ("`i'") ("4") ("N")  (`N_q4_`i'_1') (`N_q4_`i'_2') (`N_q4_`i'_3') (`N_q4_`i'_4') (`N_q4_`i'_5') (`N_q4_`i'_6') (`N_q4_`i'_7')
	post `table' ("`i'") ("4") ("f41") (`f_q41_`i'_1') (`f_q41_`i'_2') (`f_q41_`i'_3') (`f_q41_`i'_4') (`f_q41_`i'_5') (`f_q41_`i'_6') (`f_q41_`i'_7') 
	post `table' ("`i'") ("4") ("f42") (`f_q42_`i'_1') (`f_q42_`i'_2') (`f_q42_`i'_3') (`f_q42_`i'_4') (`f_q42_`i'_5') (`f_q42_`i'_6') (`f_q42_`i'_7') 
	post `table' ("`i'") ("4") ("f43") (`f_q43_`i'_1') (`f_q43_`i'_2') (`f_q43_`i'_3') (`f_q43_`i'_4') (`f_q43_`i'_5') (`f_q43_`i'_6') (`f_q43_`i'_7') 
	post `table' ("`i'") ("4") ("f44") (`f_q44_`i'_1') (`f_q44_`i'_2') (`f_q44_`i'_3') (`f_q44_`i'_4') (`f_q44_`i'_5') (`f_q44_`i'_6') (`f_q44_`i'_7')
}

postclose `table'

}

